import { defineStore } from 'pinia'
import { ref } from 'vue'
import Cookies from 'js-cookie'

const useAppStore = defineStore('app', () => {
  const locale = ref('zhCN') // zhCN enUs
  const sidebar = ref({
    opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
    withoutAnimation: false,
    hide: false
  })
  const device = ref('desktop')
  const size = ref(Cookies.get('size') || 'default')

  /**
   * 切换语言
   */
  const setLocal = () => {
    locale.value = locale.value === 'zhCN' ? 'enUs' : 'zhCN'
  }

  /**
   * 切换侧边栏
   */
  const toggleSideBar = (withoutAnimation) => {
    if (this.sidebar.hide) return false
    sidebar.value.opened = !sidebar.value.opened
    sidebar.value.withoutAnimation = withoutAnimation
    if (sidebar.value.opened) {
      Cookies.set('sidebarStatus', 1)
    } else {
      Cookies.set('sidebarStatus', 0)
    }
  }

  /**
   * 关闭侧边栏
   */
  const closeSideBar = (withoutAnimation) => {
    Cookies.set('sidebarStatus', 0)
    sidebar.value.opened = false
    sidebar.value.withoutAnimation = withoutAnimation
  }

  /**
   * 切换设备类型
   */
  const toggleDevice = (value) => (device.value = value)

  /**
   * 切换侧边栏隐藏
   */
  const toggleSideBarHide = (value) => (sidebar.value.hide = value)

  /**
   * 设置尺寸
   */
  const setSize = (value) => {
    size.value = value
    Cookies.set('size', size)
  }
  return { locale, sidebar, device, size, setLocal, toggleSideBar, closeSideBar, toggleDevice, toggleSideBarHide, setSize }
})

export default useAppStore
